# class SingleTon(object):
#     _instance = {}
#
#     def __new__(cls, *args, **kwargs):
#         if not hasattr(cls,'_instance'):
#             cls._instance = super(SingleTon,cls).__new__(cls,*args,**kwargs)
#         return cls._instance
#
#
# class MyClass(SingleTon):
#     a = 1


# class Borg(object):
#     _state = {}
#     def __new__(cls, *args, **kw):
#         ob = super(Borg, cls).__new__(cls, *args, **kw)
#         ob.__dict__ = cls._state
#         return ob
#
# class MyClass(Borg):
#     a = 1


# def Singleton(cls,*args,**kw):
#     instance = {}
#
#     def _singleton():
#         if cls not in  instance:
#             instance[cls] = cls(*args, **kw)
#         return instance[cls]
#     return _singleton
#
#
# @Singleton
# class MyClass(object):
#     a = 1
#
#
# @Singleton
# class MyClass1(object):
#     a = 1


# mysingleton.py
# class My_Singleton(object):
#     def foo(self):
#         pass
# my_singleton = My_Singleton()

# to use
# from .mysingleton import my_singleton,my_singleton1
# print(my_singleton1 is my_singleton)
# my_singleton.foo()
# my_singleton1.foo()
#
#


class Singleton(type):
    def __init__(cls, *args, **kwargs):
        cls.__instance = None
        super().__init__(*args, **kwargs)

    def __call__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super().__call__(*args, **kwargs)
            return cls.__instance
        else:
            return cls.__instance


class Spam(metaclass=Singleton):

    def __new__(cls):
        print('Spam __new__ running')
        return super().__new__(cls)

    def __init__(self):
        print('Spam __init__ running')


if __name__ == '__main__':
    a = Spam()
    b = Spam()
    print(a is b)
    c = Spam()
    print(a is c)